package com.music.peacerhythms.Uakj;


import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Handler;
import android.util.Log;


import com.music.peacerhythms.info.MusicInfo;
import com.music.peacerhythms.widget.WidgetProvider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SoundPoolManager {

   public static   boolean ispalying;
    private static volatile SoundPoolManager instance;
    private static SoundPool soundpool;
    Context contexts;
    public static Map<Integer, Integer> soundmap = new HashMap<Integer, Integer>();

    private SoundPoolManager() {
        if (Build.VERSION.SDK_INT > 21) {
            SoundPool.Builder builder = new SoundPool.Builder();

            builder.setMaxStreams(9);

            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();

            attrBuilder.setLegacyStreamType(AudioManager.STREAM_SYSTEM);//STREAM_MUSIC

            builder.setAudioAttributes(attrBuilder.build());
            soundpool = builder.build();
        } else {
            soundpool = new SoundPool(9, AudioManager.STREAM_SYSTEM, 0);
        }
    }


    public static SoundPoolManager getAppManager() {

        if (instance  ==null){
            synchronized (SoundPoolManager.class) {
                if (instance == null) {
                    instance = new SoundPoolManager();
                }


            }
        }
        return instance;
    }


    public void addSound(Context context, int positon, int resourse) {
        soundmap.put(positon, soundpool.load(context, resourse, 1));


    }

    public void addSound(Context context, int positon, String path) {
        soundmap.put(positon, soundpool.load(path, 1));


    }


    boolean isfirst;
    int second;



    private List<Integer> stopList = new ArrayList<>();

    public void play(List<MusicInfo> checkmusicInfos, Context context) {
            pause();
        soundmap.clear();
        stopList.clear();
        for (MusicInfo musicInfo : checkmusicInfos
        ) {
            if (musicInfo.israw) {
                addSound(context, musicInfo.musicid, musicInfo.rawpath);
            } else {
                addSound(context, musicInfo.musicid, musicInfo.filepath);
            }
        }
        if (!isfirst) second = 3500;
        else second = 1500;
        ispalying=true;
        isfirst = true;
                soundpool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                    @Override
                    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                            Log.e("aaaa",sampleId+"a"+status);
                        for (MusicInfo musicInfo : checkmusicInfos
                        ) {
                            int streamId = soundpool.play(soundmap.get(musicInfo.musicid), musicInfo.volume, musicInfo.volume, 0, -1, 1);
                            stopList.add(streamId);
                            Log.e("aaaa", musicInfo.musicid + musicInfo.musicname);
                        }

                    }
                });


    }

    public void playRain() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                soundpool.play(soundmap.get(1), 1, 1, 0, -1, 1);

            }
        }, 1000);
    }

    public void pause() {
        //  soundpool.autoPause();
        ispalying=false;
        if (stopList.size() > 0) {
            for (Integer integer : stopList
            ) {
                soundpool.pause(integer);

            }
        }
    }




    public void autoPause() {
          soundpool.autoPause();
        ispalying=false;
    }

    public void autoResume() {
          soundpool.autoResume();
        ispalying=true;
    }




    public void destroy() {

        soundpool.autoPause();
        soundpool.release();
        instance = null;
    }


}